<template>
  <div class="question-statistics">
    <div class="phone-night-tips">
      <p>该页面在夜间模式显示效果不好，请尽量在非夜间模式查看。</p>
    </div>
    <div v-if="error" class="question-statistics-error" style="height: 100%">
      <mu-alert color="error">
        <mu-icon left value="warning"></mu-icon>
        <span>系统题库状态读取失败，请联系系统管理员：</span>
        <span>{{ error.message }}</span>
      </mu-alert>
    </div>
    <div v-else class="question-statistics-charts">
      <BarChartByType :source="type" />
      <PieChartByStyle :source="style" />
      <PieChartByStatus :source="status" />
      <PieChartByStatus :source="mineStatus" title="我出的题" />
    </div>
  </div>
</template>

<script>
import api, { ApiError } from '@/api'
import BarChartByType from './BarChartByType'
import PieChartByStyle from './PieChartByStyle'
import PieChartByStatus from './PieChartByStatus'

export default {
  name: 'QuestionStatistics',
  components: {
    BarChartByType,
    PieChartByStyle,
    PieChartByStatus
  },
  data() {
    return {
      type: [],
      style: [],
      status: [],
      mineStatus: [],
      error: null
    }
  },
  created() {
    this.getStatistics()
  },
  mounted() {

  },
  methods: {
    // 读取题目统计数据
    async getStatistics() {
      try {
        const data = await api.getQuestionStatistics()
        this.type = data.type
        this.style = data.style
        this.status = data.status
        this.mineStatus = data.mine_status
      } catch (err) {
        if (err instanceof ApiError && err.code === 501) {
          this.error = err
        } else {
          this.$apiCatch(err)
        }
      }
    }
  }
}
</script>

<style>
.qs-chart {
  width: 500px;
  height: 400px;
}
.is-phone .qs-chart {
  width: 100%;
  height: 240px;
}
</style>

<style scoped>
.question-statistics-error {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
}
.question-statistics-charts {
  padding: 20px;
  display: flex;
  flex-wrap: wrap;
}
.phone-night-tips {
  display: none;
  position: fixed;
  z-index: 1000;
  padding: 12px;
}
.is-night .phone-night-tips {
  display: block;
}
</style>
